package permission_pb

import (
	context "context"
	fmt "fmt"
	gorm1 "github.com/infobloxopen/atlas-app-toolkit/gorm"
	query "github.com/infobloxopen/atlas-app-toolkit/query"
	errors "github.com/infobloxopen/protoc-gen-gorm/errors"
	gorm "github.com/jinzhu/gorm"
	field_mask "google.golang.org/genproto/protobuf/field_mask"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
	strings "strings"
	time "time"
)

type MenuORM struct {
	Component string
	CreatedAt *time.Time
	Hidden    bool
	Icon      string
	Id        uint64
	KeepAlive bool
	MenuLevel uint64
	Name      string
	ParentId  uint64
	Path      string
	Sort      uint64
	Title     string
	UpdatedAt *time.Time
}

// TableName overrides the default tablename generated by GORM
func (MenuORM) TableName() string {
	return "menus"
}

// ToORM runs the BeforeToORM hook if present, converts the fields of this
// object to ORM format, runs the AfterToORM hook, then returns the ORM object
func (m *Menu) ToORM(ctx context.Context) (MenuORM, error) {
	to := MenuORM{}
	var err error
	if prehook, ok := interface{}(m).(MenuWithBeforeToORM); ok {
		if err = prehook.BeforeToORM(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		t := m.CreatedAt.AsTime()
		to.CreatedAt = &t
	}
	if m.UpdatedAt != nil {
		t := m.UpdatedAt.AsTime()
		to.UpdatedAt = &t
	}
	to.MenuLevel = m.MenuLevel
	to.ParentId = m.ParentId
	to.Path = m.Path
	to.Name = m.Name
	to.Hidden = m.Hidden
	to.Component = m.Component
	to.Sort = m.Sort
	to.Title = m.Title
	to.Icon = m.Icon
	to.KeepAlive = m.KeepAlive
	if posthook, ok := interface{}(m).(MenuWithAfterToORM); ok {
		err = posthook.AfterToORM(ctx, &to)
	}
	return to, err
}

// ToPB runs the BeforeToPB hook if present, converts the fields of this
// object to PB format, runs the AfterToPB hook, then returns the PB object
func (m *MenuORM) ToPB(ctx context.Context) (Menu, error) {
	to := Menu{}
	var err error
	if prehook, ok := interface{}(m).(MenuWithBeforeToPB); ok {
		if err = prehook.BeforeToPB(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		to.CreatedAt = timestamppb.New(*m.CreatedAt)
	}
	if m.UpdatedAt != nil {
		to.UpdatedAt = timestamppb.New(*m.UpdatedAt)
	}
	to.MenuLevel = m.MenuLevel
	to.ParentId = m.ParentId
	to.Path = m.Path
	to.Name = m.Name
	to.Hidden = m.Hidden
	to.Component = m.Component
	to.Sort = m.Sort
	to.Title = m.Title
	to.Icon = m.Icon
	to.KeepAlive = m.KeepAlive
	if posthook, ok := interface{}(m).(MenuWithAfterToPB); ok {
		err = posthook.AfterToPB(ctx, &to)
	}
	return to, err
}

// The following are interfaces you can implement for special behavior during ORM/PB conversions
// of type Menu the arg will be the target, the caller the one being converted from

// MenuBeforeToORM called before default ToORM code
type MenuWithBeforeToORM interface {
	BeforeToORM(context.Context, *MenuORM) error
}

// MenuAfterToORM called after default ToORM code
type MenuWithAfterToORM interface {
	AfterToORM(context.Context, *MenuORM) error
}

// MenuBeforeToPB called before default ToPB code
type MenuWithBeforeToPB interface {
	BeforeToPB(context.Context, *Menu) error
}

// MenuAfterToPB called after default ToPB code
type MenuWithAfterToPB interface {
	AfterToPB(context.Context, *Menu) error
}

// DefaultCreateMenu executes a basic gorm create call
func DefaultCreateMenu(ctx context.Context, in *Menu, db *gorm.DB) (*Menu, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeCreate_); ok {
		if db, err = hook.BeforeCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Create(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithAfterCreate_); ok {
		if err = hook.AfterCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	return &pbResponse, err
}

type MenuORMWithBeforeCreate_ interface {
	BeforeCreate_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type MenuORMWithAfterCreate_ interface {
	AfterCreate_(context.Context, *gorm.DB) error
}

func DefaultReadMenu(ctx context.Context, in *Menu, db *gorm.DB, fs *query.FieldSelection) (*Menu, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if ormObj.Id == 0 {
		return nil, errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeReadApplyQuery); ok {
		if db, err = hook.BeforeReadApplyQuery(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	if db, err = gorm1.ApplyFieldSelection(ctx, db, fs, &MenuORM{}); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeReadFind); ok {
		if db, err = hook.BeforeReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	ormResponse := MenuORM{}
	if err = db.Where(&ormObj).First(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormResponse).(MenuORMWithAfterReadFind); ok {
		if err = hook.AfterReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormResponse.ToPB(ctx)
	return &pbResponse, err
}

type MenuORMWithBeforeReadApplyQuery interface {
	BeforeReadApplyQuery(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type MenuORMWithBeforeReadFind interface {
	BeforeReadFind(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type MenuORMWithAfterReadFind interface {
	AfterReadFind(context.Context, *gorm.DB, *query.FieldSelection) error
}

func DefaultDeleteMenu(ctx context.Context, in *Menu, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return err
	}
	if ormObj.Id == 0 {
		return errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeDelete_); ok {
		if db, err = hook.BeforeDelete_(ctx, db); err != nil {
			return err
		}
	}
	err = db.Where(&ormObj).Delete(&MenuORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithAfterDelete_); ok {
		err = hook.AfterDelete_(ctx, db)
	}
	return err
}

type MenuORMWithBeforeDelete_ interface {
	BeforeDelete_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type MenuORMWithAfterDelete_ interface {
	AfterDelete_(context.Context, *gorm.DB) error
}

func DefaultDeleteMenuSet(ctx context.Context, in []*Menu, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	var err error
	keys := []uint64{}
	for _, obj := range in {
		ormObj, err := obj.ToORM(ctx)
		if err != nil {
			return err
		}
		if ormObj.Id == 0 {
			return errors.EmptyIdError
		}
		keys = append(keys, ormObj.Id)
	}
	if hook, ok := (interface{}(&MenuORM{})).(MenuORMWithBeforeDeleteSet); ok {
		if db, err = hook.BeforeDeleteSet(ctx, in, db); err != nil {
			return err
		}
	}
	err = db.Where("id in (?)", keys).Delete(&MenuORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := (interface{}(&MenuORM{})).(MenuORMWithAfterDeleteSet); ok {
		err = hook.AfterDeleteSet(ctx, in, db)
	}
	return err
}

type MenuORMWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, []*Menu, *gorm.DB) (*gorm.DB, error)
}
type MenuORMWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, []*Menu, *gorm.DB) error
}

// DefaultStrictUpdateMenu clears / replaces / appends first level 1:many children and then executes a gorm update call
func DefaultStrictUpdateMenu(ctx context.Context, in *Menu, db *gorm.DB) (*Menu, error) {
	if in == nil {
		return nil, fmt.Errorf("Nil argument to DefaultStrictUpdateMenu")
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	lockedRow := &MenuORM{}
	db.Model(&ormObj).Set("gorm:query_option", "FOR UPDATE").Where("id=?", ormObj.Id).First(lockedRow)
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeStrictUpdateCleanup); ok {
		if db, err = hook.BeforeStrictUpdateCleanup(ctx, db); err != nil {
			return nil, err
		}
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeStrictUpdateSave); ok {
		if db, err = hook.BeforeStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Save(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithAfterStrictUpdateSave); ok {
		if err = hook.AfterStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	if err != nil {
		return nil, err
	}
	return &pbResponse, err
}

type MenuORMWithBeforeStrictUpdateCleanup interface {
	BeforeStrictUpdateCleanup(context.Context, *gorm.DB) (*gorm.DB, error)
}
type MenuORMWithBeforeStrictUpdateSave interface {
	BeforeStrictUpdateSave(context.Context, *gorm.DB) (*gorm.DB, error)
}
type MenuORMWithAfterStrictUpdateSave interface {
	AfterStrictUpdateSave(context.Context, *gorm.DB) error
}

// DefaultPatchMenu executes a basic gorm update call with patch behavior
func DefaultPatchMenu(ctx context.Context, in *Menu, updateMask *field_mask.FieldMask, db *gorm.DB) (*Menu, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	var pbObj Menu
	var err error
	if hook, ok := interface{}(&pbObj).(MenuWithBeforePatchRead); ok {
		if db, err = hook.BeforePatchRead(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbReadRes, err := DefaultReadMenu(ctx, &Menu{Id: in.GetId()}, db, nil)
	if err != nil {
		return nil, err
	}
	pbObj = *pbReadRes
	if hook, ok := interface{}(&pbObj).(MenuWithBeforePatchApplyFieldMask); ok {
		if db, err = hook.BeforePatchApplyFieldMask(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	if _, err := DefaultApplyFieldMaskMenu(ctx, &pbObj, in, updateMask, "", db); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&pbObj).(MenuWithBeforePatchSave); ok {
		if db, err = hook.BeforePatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := DefaultStrictUpdateMenu(ctx, &pbObj, db)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(pbResponse).(MenuWithAfterPatchSave); ok {
		if err = hook.AfterPatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	return pbResponse, nil
}

type MenuWithBeforePatchRead interface {
	BeforePatchRead(context.Context, *Menu, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type MenuWithBeforePatchApplyFieldMask interface {
	BeforePatchApplyFieldMask(context.Context, *Menu, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type MenuWithBeforePatchSave interface {
	BeforePatchSave(context.Context, *Menu, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type MenuWithAfterPatchSave interface {
	AfterPatchSave(context.Context, *Menu, *field_mask.FieldMask, *gorm.DB) error
}

// DefaultPatchSetMenu executes a bulk gorm update call with patch behavior
func DefaultPatchSetMenu(ctx context.Context, objects []*Menu, updateMasks []*field_mask.FieldMask, db *gorm.DB) ([]*Menu, error) {
	if len(objects) != len(updateMasks) {
		return nil, fmt.Errorf(errors.BadRepeatedFieldMaskTpl, len(updateMasks), len(objects))
	}

	results := make([]*Menu, 0, len(objects))
	for i, patcher := range objects {
		pbResponse, err := DefaultPatchMenu(ctx, patcher, updateMasks[i], db)
		if err != nil {
			return nil, err
		}

		results = append(results, pbResponse)
	}

	return results, nil
}

// DefaultApplyFieldMaskMenu patches an pbObject with patcher according to a field mask.
func DefaultApplyFieldMaskMenu(ctx context.Context, patchee *Menu, patcher *Menu, updateMask *field_mask.FieldMask, prefix string, db *gorm.DB) (*Menu, error) {
	if patcher == nil {
		return nil, nil
	} else if patchee == nil {
		return nil, errors.NilArgumentError
	}
	var err error
	var updatedCreatedAt bool
	var updatedUpdatedAt bool
	for i, f := range updateMask.Paths {
		if f == prefix+"Id" {
			patchee.Id = patcher.Id
			continue
		}
		if !updatedCreatedAt && strings.HasPrefix(f, prefix+"CreatedAt.") {
			if patcher.CreatedAt == nil {
				patchee.CreatedAt = nil
				continue
			}
			if patchee.CreatedAt == nil {
				patchee.CreatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"CreatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.CreatedAt, patchee.CreatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"CreatedAt" {
			updatedCreatedAt = true
			patchee.CreatedAt = patcher.CreatedAt
			continue
		}
		if !updatedUpdatedAt && strings.HasPrefix(f, prefix+"UpdatedAt.") {
			if patcher.UpdatedAt == nil {
				patchee.UpdatedAt = nil
				continue
			}
			if patchee.UpdatedAt == nil {
				patchee.UpdatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"UpdatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.UpdatedAt, patchee.UpdatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"UpdatedAt" {
			updatedUpdatedAt = true
			patchee.UpdatedAt = patcher.UpdatedAt
			continue
		}
		if f == prefix+"MenuLevel" {
			patchee.MenuLevel = patcher.MenuLevel
			continue
		}
		if f == prefix+"ParentId" {
			patchee.ParentId = patcher.ParentId
			continue
		}
		if f == prefix+"Path" {
			patchee.Path = patcher.Path
			continue
		}
		if f == prefix+"Name" {
			patchee.Name = patcher.Name
			continue
		}
		if f == prefix+"Hidden" {
			patchee.Hidden = patcher.Hidden
			continue
		}
		if f == prefix+"Component" {
			patchee.Component = patcher.Component
			continue
		}
		if f == prefix+"Sort" {
			patchee.Sort = patcher.Sort
			continue
		}
		if f == prefix+"Title" {
			patchee.Title = patcher.Title
			continue
		}
		if f == prefix+"Icon" {
			patchee.Icon = patcher.Icon
			continue
		}
		if f == prefix+"KeepAlive" {
			patchee.KeepAlive = patcher.KeepAlive
			continue
		}
	}
	if err != nil {
		return nil, err
	}
	return patchee, nil
}

// DefaultListMenu executes a gorm list call
func DefaultListMenu(ctx context.Context, db *gorm.DB, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) ([]*Menu, error) {
	in := Menu{}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeListApplyQuery); ok {
		if db, err = hook.BeforeListApplyQuery(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db, err = gorm1.ApplyCollectionOperators(ctx, db, &MenuORM{}, &Menu{}, f, s, p, fs)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithBeforeListFind); ok {
		if db, err = hook.BeforeListFind(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db = db.Where(&ormObj)
	db = db.Order("id")
	ormResponse := []MenuORM{}
	if err := db.Find(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(MenuORMWithAfterListFind); ok {
		if err = hook.AfterListFind(ctx, db, &ormResponse, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	pbResponse := []*Menu{}
	for _, responseEntry := range ormResponse {
		temp, err := responseEntry.ToPB(ctx)
		if err != nil {
			return nil, err
		}
		pbResponse = append(pbResponse, &temp)
	}
	return pbResponse, nil
}

type MenuORMWithBeforeListApplyQuery interface {
	BeforeListApplyQuery(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type MenuORMWithBeforeListFind interface {
	BeforeListFind(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type MenuORMWithAfterListFind interface {
	AfterListFind(context.Context, *gorm.DB, *[]MenuORM, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) error
}
type MenuServiceDefaultServer struct {
	DB *gorm.DB
}

// Create ...
func (m *MenuServiceDefaultServer) Create(ctx context.Context, in *CreateMenuRequest) (*CreateMenuResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(MenuServiceMenuWithBeforeCreate); ok {
		var err error
		if db, err = custom.BeforeCreate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultCreateMenu(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &CreateMenuResponse{Result: res}
	if custom, ok := interface{}(in).(MenuServiceMenuWithAfterCreate); ok {
		var err error
		if err = custom.AfterCreate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// MenuServiceMenuWithBeforeCreate called before DefaultCreateMenu in the default Create handler
type MenuServiceMenuWithBeforeCreate interface {
	BeforeCreate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// MenuServiceMenuWithAfterCreate called before DefaultCreateMenu in the default Create handler
type MenuServiceMenuWithAfterCreate interface {
	AfterCreate(context.Context, *CreateMenuResponse, *gorm.DB) error
}

// Read ...
func (m *MenuServiceDefaultServer) Read(ctx context.Context, in *ReadMenuRequest) (*ReadMenuResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(MenuServiceMenuWithBeforeRead); ok {
		var err error
		if db, err = custom.BeforeRead(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultReadMenu(ctx, &Menu{Id: in.GetId()}, db, in.Fields)
	if err != nil {
		return nil, err
	}
	out := &ReadMenuResponse{Result: res}
	if custom, ok := interface{}(in).(MenuServiceMenuWithAfterRead); ok {
		var err error
		if err = custom.AfterRead(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// MenuServiceMenuWithBeforeRead called before DefaultReadMenu in the default Read handler
type MenuServiceMenuWithBeforeRead interface {
	BeforeRead(context.Context, *gorm.DB) (*gorm.DB, error)
}

// MenuServiceMenuWithAfterRead called before DefaultReadMenu in the default Read handler
type MenuServiceMenuWithAfterRead interface {
	AfterRead(context.Context, *ReadMenuResponse, *gorm.DB) error
}

// Update ...
func (m *MenuServiceDefaultServer) Update(ctx context.Context, in *UpdateMenuRequest) (*UpdateMenuResponse, error) {
	var err error
	var res *Menu
	db := m.DB
	if custom, ok := interface{}(in).(MenuServiceMenuWithBeforeUpdate); ok {
		var err error
		if db, err = custom.BeforeUpdate(ctx, db); err != nil {
			return nil, err
		}
	}
	if in.GetGerogeriGegege() == nil {
		res, err = DefaultStrictUpdateMenu(ctx, in.GetPayload(), db)
	} else {
		res, err = DefaultPatchMenu(ctx, in.GetPayload(), in.GetGerogeriGegege(), db)
	}
	if err != nil {
		return nil, err
	}
	out := &UpdateMenuResponse{Result: res}
	if custom, ok := interface{}(in).(MenuServiceMenuWithAfterUpdate); ok {
		var err error
		if err = custom.AfterUpdate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// MenuServiceMenuWithBeforeUpdate called before DefaultUpdateMenu in the default Update handler
type MenuServiceMenuWithBeforeUpdate interface {
	BeforeUpdate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// MenuServiceMenuWithAfterUpdate called before DefaultUpdateMenu in the default Update handler
type MenuServiceMenuWithAfterUpdate interface {
	AfterUpdate(context.Context, *UpdateMenuResponse, *gorm.DB) error
}

// UpdateSet ...
func (m *MenuServiceDefaultServer) UpdateSet(ctx context.Context, in *UpdateSetMenuRequest) (*UpdateSetMenuResponse, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}

	db := m.DB

	if custom, ok := interface{}(in).(MenuServiceMenuWithBeforeUpdateSet); ok {
		var err error
		if db, err = custom.BeforeUpdateSet(ctx, db); err != nil {
			return nil, err
		}
	}

	res, err := DefaultPatchSetMenu(ctx, in.GetObjects(), in.GetMasks(), db)
	if err != nil {
		return nil, err
	}

	out := &UpdateSetMenuResponse{Results: res}

	if custom, ok := interface{}(in).(MenuServiceMenuWithAfterUpdateSet); ok {
		var err error
		if err = custom.AfterUpdateSet(ctx, out, db); err != nil {
			return nil, err
		}
	}

	return out, nil
}

// MenuServiceMenuWithBeforeUpdateSet called before DefaultUpdateSetMenu in the default UpdateSet handler
type MenuServiceMenuWithBeforeUpdateSet interface {
	BeforeUpdateSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// MenuServiceMenuWithAfterUpdateSet called before DefaultUpdateSetMenu in the default UpdateSet handler
type MenuServiceMenuWithAfterUpdateSet interface {
	AfterUpdateSet(context.Context, *UpdateSetMenuResponse, *gorm.DB) error
}

// List ...
func (m *MenuServiceDefaultServer) List(ctx context.Context, in *ListMenuRequest) (*ListMenuResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(MenuServiceMenuWithBeforeList); ok {
		var err error
		if db, err = custom.BeforeList(ctx, db); err != nil {
			return nil, err
		}
	}
	pagedRequest := false
	if in.GetPaging().GetLimit() >= 1 {
		in.Paging.Limit++
		pagedRequest = true
	}
	res, err := DefaultListMenu(ctx, db, in.Filter, in.OrderBy, in.Paging, in.Fields)
	if err != nil {
		return nil, err
	}
	var resPaging *query.PageInfo
	if pagedRequest {
		var offset int32
		var size int32 = int32(len(res))
		if size == in.GetPaging().GetLimit() {
			size--
			res = res[:size]
			offset = in.GetPaging().GetOffset() + size
		}
		resPaging = &query.PageInfo{Offset: offset}
	}
	out := &ListMenuResponse{Results: res, PageInfo: resPaging}
	if custom, ok := interface{}(in).(MenuServiceMenuWithAfterList); ok {
		var err error
		if err = custom.AfterList(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// MenuServiceMenuWithBeforeList called before DefaultListMenu in the default List handler
type MenuServiceMenuWithBeforeList interface {
	BeforeList(context.Context, *gorm.DB) (*gorm.DB, error)
}

// MenuServiceMenuWithAfterList called before DefaultListMenu in the default List handler
type MenuServiceMenuWithAfterList interface {
	AfterList(context.Context, *ListMenuResponse, *gorm.DB) error
}

// Delete ...
func (m *MenuServiceDefaultServer) Delete(ctx context.Context, in *DeleteMenuRequest) (*DeleteMenuResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(MenuServiceMenuWithBeforeDelete); ok {
		var err error
		if db, err = custom.BeforeDelete(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteMenu(ctx, &Menu{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteMenuResponse{}
	if custom, ok := interface{}(in).(MenuServiceMenuWithAfterDelete); ok {
		var err error
		if err = custom.AfterDelete(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// MenuServiceMenuWithBeforeDelete called before DefaultDeleteMenu in the default Delete handler
type MenuServiceMenuWithBeforeDelete interface {
	BeforeDelete(context.Context, *gorm.DB) (*gorm.DB, error)
}

// MenuServiceMenuWithAfterDelete called before DefaultDeleteMenu in the default Delete handler
type MenuServiceMenuWithAfterDelete interface {
	AfterDelete(context.Context, *DeleteMenuResponse, *gorm.DB) error
}

// DeleteSet ...
func (m *MenuServiceDefaultServer) DeleteSet(ctx context.Context, in *DeletesMenuRequest) (*DeleteMenuResponse, error) {
	db := m.DB
	objs := []*Menu{}
	for _, id := range in.Ids {
		objs = append(objs, &Menu{Id: id})
	}
	if custom, ok := interface{}(in).(MenuServiceMenuWithBeforeDeleteSet); ok {
		var err error
		if db, err = custom.BeforeDeleteSet(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteMenuSet(ctx, objs, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteMenuResponse{}
	if custom, ok := interface{}(in).(MenuServiceMenuWithAfterDeleteSet); ok {
		var err error
		if err = custom.AfterDeleteSet(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// MenuServiceMenuWithBeforeDeleteSet called before DefaultDeleteSetMenu in the default DeleteSet handler
type MenuServiceMenuWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// MenuServiceMenuWithAfterDeleteSet called before DefaultDeleteSetMenu in the default DeleteSet handler
type MenuServiceMenuWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, *DeleteMenuResponse, *gorm.DB) error
}
